<?php
/**
 * Orinoco Framework - a lightweight MVC framework.
 * http://code.google.com/p/orinoco-framework/
 *  
 * Copyright (c) 2010 Ryan Yonzon, http://ryan.rawswift.com/
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 */

class Router {
	
	public static $_route_table = array(); // route table map
	public static $_request; // current URI request
	public static $_controller; // controller
	public static $_action; // action/method
	
	public function __construct() {
		self::$_request = $_SERVER['REQUEST_URI']; // get current URI request
	}
	
	public function add($_uri, $_method_map) {
		$_uri_split = preg_split("/\?/", $_uri); // split

		$_uri = $_uri_split[0]; // store the value for route comparison later
		$_map = preg_split("/\//", $_uri, 0, PREG_SPLIT_NO_EMPTY); // get resources
		
		if (!isset($_map[0])) {
			self::$_route_table[DEFAULT_CONTROLLER][DEFAULT_ACTION] = $_method_map;
		} else {	
			if (!isset($_map[1])) {
				self::$_route_table[$_map[0]][DEFAULT_ACTION] = $_method_map;
			} else {
				self::$_route_table[$_map[0]][$_map[1]] = $_method_map;
			} 
		}
	}
	
	public function getController() {
		if (isset(self::$_controller)) { // check if we already have a controller
			return self::$_controller; // return current controller
		} else { // else process controller and action request
			$_split = preg_split("/\?/", self::$_request); // split request
			$_uri = $_split[0];
			$_map = preg_split("/\//", $_uri, 0, PREG_SPLIT_NO_EMPTY);
			
			if (!isset($_map[0])) { // check if this is index URI (e.g. http://sub.domain.tld/)
				if (isset(self::$_route_table[DEFAULT_CONTROLLER])) { // check if we have an default controller (e.g. index) on the route table
					$_request = self::$_route_table[DEFAULT_CONTROLLER][DEFAULT_ACTION];
					self::$_controller = $_request["controller"];
					self::$_action = $_request["action"];
				} else { // we don't have a default resource, send a 404 header error
					header("HTTP/1.1 404 Not Found");
					echo 'Router Error: Controller "' . DEFAULT_CONTROLLER . '" Not Found.';
					exit();
				}
			} else { // No, we are not index URI
				if (!isset($_map[1])) { // check that we don't have an action/method on URI (e.g. /controller/action-name)
					if (!isset(self::$_route_table[$_map[0]][DEFAULT_ACTION])) { // check for default action/method (e.g. index)
						/* TO-DO: find a better error message especially on debug mode */
						header("HTTP/1.1 404 Not Found");
						echo 'Router Error: Controller "' . $_map[0] . '" Not Found.';
						exit();
					} else { // Yes, there is a default service (e.g. index)
						$_request = self::$_route_table[$_map[0]][DEFAULT_ACTION];
						self::$_controller = $_request["controller"];
						self::$_action = $_request["action"];
					}
				} else { // we have an action/method (e.g. /controller/action-name)
					if (!isset(self::$_route_table[$_map[0]][$_map[1]])) { // check if don't have a action/method request on route table
						/* TO-DO: find a better error message especially on debug mode */
						header("HTTP/1.1 404 Not Found");
						echo 'Router Error: Action "' . $_map[1] . '" Not Found.';
						exit();
					} else { // Yes, we do have an action/method request!
						$_request = self::$_route_table[$_map[0]][$_map[1]];
						self::$_controller = $_request["controller"];
						self::$_action = $_request["action"];
					}					
				} 
			}			
			return self::$_controller; // return controller name
		}
	}
	
	public function getAction() {
		if (isset(self::$_action)) { // check if we already have a action/method
			return self::$_action; // return current action/method name
		} else { // throw 404 file not found header
			header("HTTP/1.1 404 Not Found");
			echo 'Router Error: Action Not Found.';
			exit();
		}			
	}
	
	public function getRouteTable() {
		return self::$_route_table;
	}
	
	public function getURIRequest() {
		return self::$_request;
	}
	
} // end class

// -EOF-